# ruff: noqa: E501
# The above line is a pragma to instruct the 'ruff' linter to ignore line length errors (E501) in this file.

# Import the Markup class from Flask, which is used to mark a string as safe for HTML rendering.
from flask import Markup
# Import the base Model class from Flask-AppBuilder.
from flask_appbuilder import Model
# Import various column types from SQLAlchemy to define the database schema.
from sqlalchemy import Column, Integer, String, Text

# Import the Flask application instance to access its configuration.
from myapp.app import app
# Import a custom base model that may contain shared logic or properties.
from myapp.models.base import MyappModelBase

# Get the metadata object from the base Model class. Metadata stores information about the database schema.
metadata = Model.metadata
# Get the application's configuration object.
conf = app.config


# Define the Aihub class, which maps to the 'aihub' table in the database.
# This model represents an entry in an AI Hub, likely a collection of AI models, tools, or datasets.
class Aihub(Model, MyappModelBase):
    # Specifies the name of the database table for this model.
    __tablename__ = 'aihub'

    # Defines the primary key column for the table, an auto-incrementing integer.
    id = Column(Integer, primary_key=True)
    # Defines a column for a universally unique identifier (UUID).
    uuid = Column(String(2000), nullable=True, default='')
    # Defines a column for the status of the AI Hub entry (e.g., 'online', 'offline').
    status = Column(String(200), nullable=True, default='')
    # Defines a column for a URL to the documentation.
    doc = Column(String(200), nullable=True, default='')
    # Defines a column for the name of the AI Hub entry.
    name = Column(String(200), nullable=True, default='')
    # Defines a column for the application field or domain (e.g., 'Finance', 'Healthcare').
    field = Column(String(200), nullable=True, default='')
    # Defines a column for the specific scenes or use cases.
    scenes = Column(String(200), nullable=True, default='')
    # Defines a column for the type of the entry (e.g., 'Model', 'Dataset', 'Notebook').
    type = Column(String(200), nullable=True, default='')
    # Defines a column for labels or tags associated with the entry.
    label = Column(String(200), nullable=True, default='')
    # Defines a column for a detailed description of the entry.
    describe = Column(String(2000), nullable=True, default='')
    # Defines a column for the source or origin of the entry.
    source = Column(String(200), nullable=True, default='')
    # Defines a column for a URL to a picture or icon.
    pic = Column(String(500), nullable=True, default='')
    # Defines a column to store information about the dataset, possibly as a JSON string or URL.
    dataset = Column(Text, nullable=True, default='')
    # Defines a column for a link to a Jupyter Notebook.
    notebook = Column(String(2000), nullable=True, default='')
    # Defines a column to store a job template, likely in JSON or YAML format.
    job_template = Column(Text, nullable=True, default='')
    # Defines a column to store a pipeline definition.
    pipeline = Column(Text, nullable=True, default='')
    # Defines a column for a link to a pre-trained model.
    pre_train_model = Column(String(2000), nullable=True, default='')
    # Defines a column for inference service configuration.
    inference = Column(Text, nullable=True, default='')
    # Defines a column for service deployment information.
    service = Column(Text, nullable=True, default='')
    # Defines a column for the version of the entry.
    version = Column(String(200), nullable=True, default='')
    # Defines a column to indicate the popularity or "hotness" of the entry.
    hot = Column(Integer, default=1)
    # Defines a column for the price or cost associated with using the entry.
    price = Column(Integer, default=1)

    # This property generates an HTML card for displaying the Aihub entry in a web interface.
    @property
    def card(self):
        """
        Generates a rich HTML representation of the Aihub item as a display card.
        The card includes an image, description, and action links.
        The links are only active if the item's status is 'online'.
        """
        # Conditionally generate the notebook URL. It's a valid URL only if status is 'online' and a notebook exists.
        notebook_url = (
            '/aihub/api/notebook/' + self.uuid if self.status == 'online' and self.notebook else ''
        )
        # Conditionally generate the training URL. It's active if status is 'online' and a template or pipeline is available.
        train_url = (
            '/aihub/api/train/' + self.uuid
            if self.status == 'online' and (self.job_template or self.pipeline)
            else ''
        )
        # Conditionally generate the service URL. It's active if status is 'online' and a service or inference config exists.
        service_url = (
            '/aihub/api/service/' + self.uuid
            if self.status == 'online' and (self.service or self.inference)
            else ''
        )

        # Use Markup() to ensure the f-string containing HTML is treated as safe and not escaped.
        return Markup(
            f"""
{/* Main container for the card. The border color changes based on the 'online' status. */}
<div style="border: 3px solid rgba({'29,152,29,.9' if self.status=='online' else '0,0,0,.2'});border-radius: 3px;">
    {/* The card's image is a clickable link to the documentation, active only when 'online'. */}
    <a href="{self.doc if self.status=='online' else ''}">
        <img src="{self.pic}" style="height:200px;width:100%" alt="{self.describe}"/>
    </a>
    <br>
    <div>
        {/* Container for the card's text content. */}
        <div class="p16" alt="{self.describe}">
            <div class="p-r card-popup ellip1">
                {/* Display the description, prefixed with its status. */}
                {("在线:" if self.status=='online' else '待上线:')+self.describe}
                {/* A hidden div that appears on hover to show the full description. */}
                <div class="p-a card-popup-target d-n" style="top:100%;left:0;background:rgba(0,0,0,0.5);color:#fff;border-radius:3px;">{self.describe}</div>
            </div>
        </div>
        {/* Container for the action buttons at the bottom of the card. */}
        <div style="border-top: 1px solid rgba(0,0,0,.06);" class="ptb8 d-f ac jc-b">
            {/* Link to the notebook. */}
            <a class="flex1 ta-c" style="border-right: 1px solid rgba(0,0,0,.06);" href='{notebook_url}'>notebook</a>
            {/* Link to the training functionality. */}
            <a class="flex1 ta-c" style="border-right: 1px solid rgba(0,0,0,.06);" href='{train_url}'>训练</a>
            {/* Link to the service/inference functionality. */}
            <a class="flex1 ta-c" style="border-right: 1px solid rgba(0,0,0,.06);" href='{service_url}'>服务</a>
        </div>
    </div>
</div>
"""
        )
